package cn.itcast.service.company.impl;

import cn.itcast.dao.company.CompanyDao;
import cn.itcast.domain.Company;
import cn.itcast.domain.PageBean;
import cn.itcast.service.company.CompanyService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CompanyServiceImpl implements CompanyService {
    @Autowired
    private CompanyDao companyDao;

    public List<Company> findAll() {
        return companyDao.findAll();
    }

    @Override
    public void save(Company company) {
        companyDao.save(company);
    }

    @Override
    public Company findById(String id) {
        return companyDao.findById(id);
    }

    @Override
    public void update(Company company) {
        companyDao.update(company);
    }

    @Override
    public void del(String id) {
        companyDao.del(id);
    }

    @Override //自己实现的分页
    public PageBean findPage(int pageNum, int pageSize) {

        // 封装页面需要的数据
        List<Company> list=companyDao.findList((pageNum-1)*pageSize,pageSize);
        // 封装总条数
        int total=companyDao.findCount();
        // 封装总页数
        int pages=0;
        if(total%pageSize==0){
            pages=total/pageSize;
        }else{
            pages=total/pageSize+1;
        }
        // 创建pageBean
        PageBean pb = new PageBean(list,pageNum,pageSize,total,pages);

        return pb;
    }

    @Override //pageHelper插件实现
    public PageInfo findPageTopageHelper(int pageNum, int pageSize) {


        PageHelper.startPage(pageNum,pageSize);
            // 紧跟在PageHelper.startPage后面的查询语句会自动分页
        List<Company> list=companyDao.findAll();
        PageInfo pb = new PageInfo(list);

        return pb;
    }
}
